﻿using System;
using System.Collections;
using System.Collections.Generic;
using Assets.Scripts.UI.Controller;
using UnityEngine;
using UnityEngine.Networking;

namespace Assets.Scripts.GtsScripts
{
	// Token: 0x020002C0 RID: 704
	public class Giantess : Humanoid
	{
		// Token: 0x17000262 RID: 610
		// (get) Token: 0x06001041 RID: 4161 RVA: 0x00077710 File Offset: 0x00075910
		// (set) Token: 0x06001042 RID: 4162 RVA: 0x00077718 File Offset: 0x00075918
		public Giantess.MorphData[] Morphs { get; private set; }

		// Token: 0x06001043 RID: 4163 RVA: 0x00002973 File Offset: 0x00000B73
		public override void OnStartAuthority()
		{
		}

		// Token: 0x06001044 RID: 4164 RVA: 0x00077724 File Offset: 0x00075924
		private void DeleteOldColliders()
		{
			SABoneColliderChild[] componentsInChildren = base.GetComponentsInChildren<SABoneColliderChild>();
			foreach (SABoneColliderChild saboneColliderChild in componentsInChildren)
			{
				UnityEngine.Object.Destroy(saboneColliderChild.gameObject);
			}
		}

		// Token: 0x06001045 RID: 4165 RVA: 0x00077760 File Offset: 0x00075960
		protected override void Awake()
		{
			Debug.Log("Giantess Awake");
			this.isGiantess = true;
			this.DeleteOldColliders();
			base.Awake();
			GiantessManager.Instance.AddGiantess(this);
			this.mBodyPhysics = base.gameObject.AddComponent<BodyPhysics>();
			this.mColliderGts = new GameObject("Collider of " + base.gameObject.name);
			this.mColliderGts.transform.position = this.myTransform.position;
			this.mColliderGts.transform.rotation = this.myTransform.rotation;
			this.mColliderGts.transform.localScale = this.myTransform.localScale;
			this.GtsMovement = this.mColliderGts.AddComponent<GTSMovement>();
			this.GtsMovement.SetGiantess(this);
			this.baseHeight = 1600f;
			this.mMdModel = base.gameObject.GetComponent<MMD4MecanimModel>();
			if (this.mMdModel != null)
			{
				this.mMdModel.pphEnabled = false;
				this.mMdModel.pphEnabledNoAnimation = false;
				this.mBlinkController = base.gameObject.AddComponent<MMD4MecanimMorphHelper>();
				this.mBlinkController.morphSpeed = 0f;
				this.mBlinkController.morphName = "笑い";
				this.mBlinkController.morphWeight = 0f;
				base.StartCoroutine("BlinkRoutine");
			}
			this.mSkinnedMeshColliderList = this.AddSkinnedMeshColliders();
			this.UpdateCapsuleColliderTransform();
			this.mColliderGts.AddComponent<GiantessObstacleDetector>();
			base.movement = this.mColliderGts.AddComponent<MovementCharacter>();
			base.movement.anim = base.GetComponent<AnimationManager>();
			base.movement.entity = this;
			Animator component = base.GetComponent<Animator>();
			component.cullingMode = AnimatorCullingMode.AlwaysAnimate;
			AnimationManager.SetAnimatorController(component, IOManager.Instance.gtsAnimatorController);
			component.SetFloat(Animator.StringToHash("animationSpeed"), GameController.globalSpeed);
			this.ik = base.GetComponent<GiantessIK>();
			base.gameObject.AddComponent<FootEffect>();
			base.gameObject.AddComponent<Voice>();
			base.gameObject.AddComponent<Wind>();
			this.newShader = Shader.Find("SB_Standard2Sided");
			this.myRenderers = base.gameObject.GetComponentsInChildren<Renderer>();
			foreach (Renderer renderer in this.myRenderers)
			{
				Material[] materials = renderer.materials;
				foreach (Material item in materials)
				{
					this.myMaterials.Add(item);
				}
			}
			this.canLookAtPlayer = true;
			if (GlobalPreferences.GtsAI.value)
			{
				this.ai.EnableAI();
			}
		}

		// Token: 0x06001046 RID: 4166 RVA: 0x00077A0F File Offset: 0x00075C0F
		public override void RegisterEntity()
		{
			if (ObjectManager.Instance != null)
			{
				ObjectManager.Instance.OnGiantessSpawned(this);
			}
			else
			{
				Debug.LogError("ObjectManager has not been instantiated yet.");
			}
		}

		// Token: 0x06001047 RID: 4167 RVA: 0x00077A3C File Offset: 0x00075C3C
		private void Update()
		{
			if (this.useAltShaders && !this.altShadersHaveBeenUsed)
			{
				this.ChangeToAltShaders();
			}
			if (this.useAltTangents && !this.altTangentsHaveBeenUsed)
			{
				this.ChangeToAltTangents();
			}
			this.maxSize = Giantess.MaxScale;
			this.minSize = Giantess.MinScale;
			this.bloodPatches.RemoveAll((GameObject blood) => blood == null);
			foreach (GameObject gameObject in this.bloodPatches)
			{
				Giantess.BloodScale component = gameObject.GetComponent<Giantess.BloodScale>();
				if ((!GlobalPreferences.BloodGrows.value && component.scale < gameObject.gameObject.transform.lossyScale.y) || (!GlobalPreferences.BloodShrinks.value && component.scale > gameObject.gameObject.transform.lossyScale.y))
				{
					base.StayAtLockedScale(gameObject.gameObject.transform, component.scale);
				}
				else
				{
					component.scale = gameObject.gameObject.transform.lossyScale.y;
				}
			}
		}

		// Token: 0x06001048 RID: 4168 RVA: 0x00077BB0 File Offset: 0x00075DB0
		private void Start()
		{
			Debug.Log("Start");
			this.SearchMorphs();
			ShaderLoader.EnableMaterialMorph(base.gameObject);
			base.movement.anim.UpdateAnimationSpeed();
			this.RegisterEntity();
		}

		// Token: 0x06001049 RID: 4169 RVA: 0x00077BE4 File Offset: 0x00075DE4
		private void ChangeToAltShaders()
		{
			ShaderLoader.ApplyShader(base.gameObject, this.newShader);
			ShaderLoader.EnableMaterialMorph(base.gameObject);
			foreach (Material item in this.myMaterials)
			{
				this.materialsThatWereChanged.Add(item);
			}
			this.altShadersHaveBeenUsed = true;
		}

		// Token: 0x0600104A RID: 4170 RVA: 0x00077C68 File Offset: 0x00075E68
		private void ChangeToAltTangents()
		{
			foreach (SkinnedMeshRenderer skinnedMeshRenderer in this.myRenderers)
			{
				Mesh sharedMesh = skinnedMeshRenderer.sharedMesh;
				sharedMesh.tangents = null;
				skinnedMeshRenderer.sharedMesh = sharedMesh;
			}
			this.altTangentsHaveBeenUsed = true;
		}

		// Token: 0x0600104B RID: 4171 RVA: 0x00077CB8 File Offset: 0x00075EB8
		private void SearchMorphs()
		{
			if (this.mMdModel != null)
			{
				this.Morphs = new Giantess.MorphData[this.mMdModel.morphList.Length];
				for (int i = 0; i < this.Morphs.Length; i++)
				{
					this.Morphs[i].Name = this.mMdModel.morphList[i].morphData.nameEn;
					if (this.Morphs[i].Name.Length == 0)
					{
						string nameJp = this.mMdModel.morphList[i].morphData.nameJp;
						this.Morphs[i].Name = this.TranslateJapanseMorph(nameJp);
					}
					this.Morphs[i].Weight = 0f;
				}
			}
			else
			{
				SkinnedMeshRenderer[] componentsInChildren = base.GetComponentsInChildren<SkinnedMeshRenderer>();
				foreach (SkinnedMeshRenderer skinnedMeshRenderer in componentsInChildren)
				{
					this.mMeshRenderer = skinnedMeshRenderer;
					Mesh sharedMesh = skinnedMeshRenderer.sharedMesh;
					if (sharedMesh.blendShapeCount != 0)
					{
						this.Morphs = new Giantess.MorphData[sharedMesh.blendShapeCount];
						for (int k = 0; k < this.Morphs.Length; k++)
						{
							this.Morphs[k].Name = sharedMesh.GetBlendShapeName(k);
							this.Morphs[k].Weight = 0f;
						}
						break;
					}
				}
				if (this.Morphs == null)
				{
					this.Morphs = new Giantess.MorphData[0];
				}
			}
		}

		// Token: 0x0600104C RID: 4172 RVA: 0x00077E5C File Offset: 0x0007605C
		public void SetMorphValue(int i, float weight)
		{
			if (this.mMdModel != null)
			{
				if (this.mMdModel.morphList == null)
				{
					Debug.LogError("Morph List has not been created yet.");
					return;
				}
				this.mMdModel.morphList[i].weight = weight;
			}
			else
			{
				this.mMeshRenderer.SetBlendShapeWeight(i, weight * 100f);
			}
			this.Morphs[i].Weight = weight;
		}

		// Token: 0x0600104D RID: 4173 RVA: 0x00077ED4 File Offset: 0x000760D4
		private string TranslateJapanseMorph(string japanese)
		{
			JapaneseData japaneseData = Resources.Load<JapaneseData>("japanese");
			if (japaneseData.translation == null)
			{
				japaneseData.translation = new List<JapaneseData.MorphTranslation>();
			}
			string translation = japaneseData.GetTranslation(japanese);
			return (!(translation == string.Empty)) ? translation : japanese;
		}

		// Token: 0x0600104E RID: 4174 RVA: 0x00077F24 File Offset: 0x00076124
		private List<SkinnedMeshCollider> AddSkinnedMeshColliders()
		{
			List<SkinnedMeshCollider> list = new List<SkinnedMeshCollider>();
			SkinnedMeshRenderer[] componentsInChildren = base.GetComponentsInChildren<SkinnedMeshRenderer>();
			foreach (SkinnedMeshRenderer skinnedMeshRenderer in componentsInChildren)
			{
				SkinnedMeshCollider skinnedMeshCollider = skinnedMeshRenderer.gameObject.AddComponent<SkinnedMeshCollider>();
				skinnedMeshCollider.Init(this);
				list.Add(skinnedMeshCollider);
			}
			return list;
		}

		// Token: 0x0600104F RID: 4175 RVA: 0x00077F7C File Offset: 0x0007617C
		private IEnumerator BlinkRoutine()
		{
			this.mBlinkController.morphSpeed = 0.05f;
			for (;;)
			{
				if (GlobalPreferences.BlinkEnabled.value)
				{
					yield return new WaitForSeconds(3f);
					this.mBlinkController.morphWeight = 1f;
					yield return new WaitForSeconds(0.05f);
					this.mBlinkController.morphWeight = 0f;
				}
				else
				{
					yield return null;
				}
			}
			yield break;
		}

		// Token: 0x06001050 RID: 4176 RVA: 0x00077F97 File Offset: 0x00076197
		public void InvokeColliderUpdate()
		{
			base.Invoke("ForceColliderUpdate", 1f);
		}

		// Token: 0x06001051 RID: 4177 RVA: 0x00077FAC File Offset: 0x000761AC
		public void ForceColliderUpdate()
		{
			foreach (SkinnedMeshCollider skinnedMeshCollider in this.mSkinnedMeshColliderList)
			{
				skinnedMeshCollider.UpdateCollider();
			}
		}

		// Token: 0x06001052 RID: 4178 RVA: 0x00078008 File Offset: 0x00076208
		public void ManualBreastPhysics()
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(Resources.Load("UI/BreastControlsUi") as GameObject);
			GameController component = Camera.main.GetComponent<GameController>();
			gameObject.transform.SetParent(component.view.mainCanvas.transform, false);
			BreastPhysicsController controller = gameObject.AddComponent<BreastPhysicsController>();
			this.mBodyPhysics.HandleNewBreastPhysics(controller);
		}

		// Token: 0x06001053 RID: 4179 RVA: 0x00078064 File Offset: 0x00076264
		public override void SetCollider(bool enable)
		{
			this.mColliderGts.gameObject.SetActive(enable);
			if (enable)
			{
				this.UpdateCapsuleColliderTransform();
			}
			this.SkinnedCollidersEnable(enable);
			this.PhysicsCollidersEnable(enable);
		}

		// Token: 0x06001054 RID: 4180 RVA: 0x00078094 File Offset: 0x00076294
		public override void SetPoseMode(bool pose)
		{
			base.SetPoseMode(pose);
			this.PhysicsCollidersEnable(!pose);
			foreach (SkinnedMeshCollider skinnedMeshCollider in this.mSkinnedMeshColliderList)
			{
				skinnedMeshCollider.SetPoseMode(pose);
			}
			this.mBodyPhysics.SetPoseMode(pose);
		}

		// Token: 0x06001055 RID: 4181 RVA: 0x00078110 File Offset: 0x00076310
		public void CreateFingerPosers()
		{
			if (this.fingerPoser != null)
			{
				UnityEngine.Object.Destroy(this.fingerPoser);
			}
			this.fingerPoser = base.gameObject.AddComponent<FingerPoser>();
			this.fingerPoser.Init(this.anim);
		}

		// Token: 0x06001056 RID: 4182 RVA: 0x00078150 File Offset: 0x00076350
		public void DestroyFingerPosers()
		{
			if (this.fingerPoser != null)
			{
				UnityEngine.Object.Destroy(this.fingerPoser);
			}
		}

		// Token: 0x06001057 RID: 4183 RVA: 0x0007816E File Offset: 0x0007636E
		public void DisplayFingerPosers(bool isVisible)
		{
			if (this.fingerPoser != null)
			{
				this.fingerPoser.ShowSpheres(isVisible);
			}
		}

		// Token: 0x06001058 RID: 4184 RVA: 0x0007818D File Offset: 0x0007638D
		private void PhysicsCollidersEnable(bool option)
		{
			if (this.mBodyPhysics && !this.mBodyPhysics.ignore)
			{
				this.mBodyPhysics.ColliderEnable(option);
			}
			this.GtsMovement.EnableCollider(option);
		}

		// Token: 0x06001059 RID: 4185 RVA: 0x000781C8 File Offset: 0x000763C8
		private void SkinnedCollidersEnable(bool option)
		{
			foreach (SkinnedMeshCollider skinnedMeshCollider in this.mSkinnedMeshColliderList)
			{
				skinnedMeshCollider.EnableCollision(option);
			}
		}

		// Token: 0x0600105A RID: 4186 RVA: 0x00078224 File Offset: 0x00076424
		private void UpdateCapsuleColliderTransform()
		{
			this.mColliderGts.transform.position = CenterOrigin.VirtualToWorld(this.virtualPosition);
			this.mColliderGts.transform.rotation = base.transform.rotation;
			this.ScaleCapsuleCollider();
		}

		// Token: 0x0600105B RID: 4187 RVA: 0x00078262 File Offset: 0x00076462
		private void ScaleCapsuleCollider()
		{
			this.mColliderGts.transform.localScale = base.transform.lossyScale;
		}

		// Token: 0x0600105C RID: 4188 RVA: 0x0007827F File Offset: 0x0007647F
		protected override void OnDestroy()
		{
			UnityEngine.Object.Destroy(this.mColliderGts);
			GiantessManager.Instance.RemoveGiantess(base.id);
			base.OnDestroy();
		}

		// Token: 0x0600105D RID: 4189 RVA: 0x000782A2 File Offset: 0x000764A2
		public override void ChangeRotation(Vector3 newRotation)
		{
			base.ChangeRotation(newRotation);
			this.UpdateCapsuleColliderTransform();
		}

		// Token: 0x0600105E RID: 4190 RVA: 0x000782B1 File Offset: 0x000764B1
		public override void ChangeScale(float newScale)
		{
			base.ChangeScale(newScale);
			this.ScaleCapsuleCollider();
			base.movement.anim.UpdateAnimationSpeed();
		}

		// Token: 0x0600105F RID: 4191 RVA: 0x000782D0 File Offset: 0x000764D0
		public override void DestroyObject(bool recursive = true)
		{
			this.mBodyPhysics.Destroy();
			UnityEngine.Object.Destroy(this.mColliderGts);
			base.DestroyObject(recursive);
			ObjectManager.Instance.RemoveGiantess(base.id);
		}

		// Token: 0x06001060 RID: 4192 RVA: 0x000782FF File Offset: 0x000764FF
		public void StartBreastExpansion()
		{
			this.mBodyPhysics.StartBe();
		}

		// Token: 0x06001061 RID: 4193 RVA: 0x0007830C File Offset: 0x0007650C
		public void SetBeSpeed(float speed)
		{
			this.mBodyPhysics.BeSpeed = speed;
		}

		// Token: 0x06001062 RID: 4194 RVA: 0x0007831C File Offset: 0x0007651C
		public void AddBlood(GameObject blood)
		{
			Giantess.BloodScale bloodScale = blood.AddComponent<Giantess.BloodScale>();
			bloodScale.scale = blood.transform.lossyScale.x;
			this.bloodPatches.Add(blood);
		}

		// Token: 0x06001065 RID: 4197 RVA: 0x00002973 File Offset: 0x00000B73
		private void UNetVersion()
		{
		}

		// Token: 0x06001066 RID: 4198 RVA: 0x00078374 File Offset: 0x00076574
		public override bool OnSerialize(NetworkWriter writer, bool forceAll)
		{
			bool flag = base.OnSerialize(writer, forceAll);
			bool flag2;
			return flag2 || flag;
		}

		// Token: 0x06001067 RID: 4199 RVA: 0x00057DE9 File Offset: 0x00055FE9
		public override void OnDeserialize(NetworkReader reader, bool initialState)
		{
			base.OnDeserialize(reader, initialState);
		}

		// Token: 0x0400150E RID: 5390
		public static float MaxScale = 1000f;

		// Token: 0x0400150F RID: 5391
		public static float MinScale = 0.001f;

		// Token: 0x04001510 RID: 5392
		private SkinnedMeshRenderer mMeshRenderer;

		// Token: 0x04001511 RID: 5393
		private MMD4MecanimMorphHelper mBlinkController;

		// Token: 0x04001512 RID: 5394
		private List<SkinnedMeshCollider> mSkinnedMeshColliderList;

		// Token: 0x04001513 RID: 5395
		private GameObject mColliderGts;

		// Token: 0x04001514 RID: 5396
		public GTSMovement GtsMovement;

		// Token: 0x04001516 RID: 5398
		private MMD4MecanimModel mMdModel;

		// Token: 0x04001517 RID: 5399
		private BodyPhysics mBodyPhysics;

		// Token: 0x04001518 RID: 5400
		private FingerPoser fingerPoser;

		// Token: 0x04001519 RID: 5401
		private List<GameObject> bloodPatches = new List<GameObject>();

		// Token: 0x0400151A RID: 5402
		private Renderer[] myRenderers;

		// Token: 0x0400151B RID: 5403
		private List<Material> myMaterials = new List<Material>();

		// Token: 0x0400151C RID: 5404
		private List<Material> materialsThatWereChanged = new List<Material>();

		// Token: 0x0400151D RID: 5405
		private bool materialsHaveBeenChanged;

		// Token: 0x0400151E RID: 5406
		private Shader newShader;

		// Token: 0x0400151F RID: 5407
		private const string alternateShader = "SB_Standard2Sided";

		// Token: 0x04001520 RID: 5408
		public bool useAltShaders;

		// Token: 0x04001521 RID: 5409
		public bool useAltTangents;

		// Token: 0x04001522 RID: 5410
		private bool altShadersHaveBeenUsed;

		// Token: 0x04001523 RID: 5411
		private bool altTangentsHaveBeenUsed;

		// Token: 0x04001524 RID: 5412
		public bool canLookAtPlayer;

		// Token: 0x020002C1 RID: 705
		public struct MorphData
		{
			// Token: 0x04001526 RID: 5414
			public string Name;

			// Token: 0x04001527 RID: 5415
			public float Weight;
		}

		// Token: 0x020002C2 RID: 706
		private class BloodScale : MonoBehaviour
		{
			// Token: 0x04001528 RID: 5416
			public float scale;
		}
	}
}
